package backend

import (
	"fmt"
	"github.com/MQEnergy/go-skeleton/internal/app/controller"
	"github.com/MQEnergy/go-skeleton/internal/app/model"
	"github.com/MQEnergy/go-skeleton/internal/app/model/request"
	"github.com/MQEnergy/go-skeleton/internal/app/query"
	"github.com/MQEnergy/go-skeleton/internal/request/company"
	"github.com/MQEnergy/go-skeleton/pkg/response"
	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
	"gorm.io/gorm"
	"strconv"
)

type CompanyController struct {
	controller.Controller
}

var Company = &CompanyController{}

func (c *CompanyController) Add(ctx *fiber.Ctx) error {
	var param = &company.Add{}
	if err := c.Validate(ctx, param); err != nil {
		return response.BadRequestException(ctx, err.Error())

	}
	//return response.SuccessJSON(ctx, "axiba", param)
	return response.BadRequestException(ctx, "ggggg")
}
func (c *CompanyController) Edit(ctx *fiber.Ctx) error {
	page := ctx.Query("page")
	var a map[string]interface{}
	ctx.BodyParser(&a)
	pagesize := ctx.Query("pagesize")
	s, ok := a["pagesize"].(string)
	fmt.Println("size:", s, ok)
	s1, ok := a["page"].(int)
	fmt.Println("page:", s1, ok)
	fmt.Println("--------", page, pagesize, a)
	ctx.Accepts("text/html")
	//u := query.Users
	//paginate := query.NewDo(u).Paginate(ctx)
	//param := &model.Company{}
	//if err := c.Validate(ctx, param); err != nil {
	//	return response.BadRequestException(ctx, err.Error())
	//}
	return response.SuccessJSON(ctx, "", "paginate")

}
func (c *CompanyController) List(ctx *fiber.Ctx) error {
	validate := validator.New()
	var user request.UserRequest
	account := ctx.Query("account")
	//account := ctx.Query("account")
	ctx.BodyParser(&user)
	log.Info("account----------", account)
	if account != "" {
		user.Account = account
	}
	err := validate.Struct(user)
	if err != nil {
		return response.BadRequestException(ctx, err.Error())
	}
	fmt.Println("err----------", err)

	var users model.Users

	page, _ := query.Page(users, ctx, ListCondition(ctx), ListCondition2(ctx))
	fmt.Println("page:", page)
	return response.SuccessJSON(ctx, "", page)
	//var company model.Company
	//page := query.Page(company, ctx, ListCondition(ctx), ListCondition2(ctx))
	//fmt.Println("page:", page)
	//return response.SuccessJSON(ctx, "", page)

}

func ListCondition(ctx *fiber.Ctx) func(db *gorm.DB) *gorm.DB {

	return func(db *gorm.DB) *gorm.DB {
		return db.Where("id>?", 2)
	}
}
func ListCondition2(ctx *fiber.Ctx) func(db *gorm.DB) *gorm.DB {

	return func(db *gorm.DB) *gorm.DB {
		return db.Where("id<?", 4)
	}
}

type BasicInterface interface {
	Name() string
	Age() int
}

func (c *CompanyController) Detail(ctx *fiber.Ctx) error {
	u := query.Users
	id := ctx.Query("id")
	atoi, _ := strconv.Atoi(id)
	detail, _ := query.NewDo(u).Where(u.ID.Eq(int32(atoi))).First()
	return response.SuccessJSON(ctx, "", detail)

}
